package com.group15.phms;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

public class Registration1 extends Activity {

	private EditText registrantNameView;
	private String registrantName="";
	private EditText registrantUserNameView;
	private String registrantUserName="";
	private EditText registrantPasswordView;
	private String registrantPassword="";
	private EditText registrantConfirmPasswordView;
	private String registrantConfirmPassword="";
	private EditText registrantEmailView;
	private String registrantEmail="";
	private DatePicker dp;
	private int day,month,year;
	SharedPreferences logins;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration1);
		
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		
		findViewById(R.id.nextButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						nextPage();
					}
				});
		findViewById(R.id.cancelButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						cancel();
					}
				});
		
		registrantNameView=(EditText)findViewById(R.id.registrantName);
		registrantUserNameView=(EditText)findViewById(R.id.username);
		registrantPasswordView =(EditText)findViewById(R.id.password);
		registrantConfirmPasswordView =(EditText)findViewById(R.id.confirmPassword);
		registrantEmailView =(EditText)findViewById(R.id.registrantEmail);
		dp=(DatePicker)findViewById(R.id.datePicker1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registration1, menu);
		return true;
	}
	
	
	//check for form errors then advance to next page
	public void nextPage() {
		
		// Reset errors.
		registrantNameView.setError(null);
		registrantName=registrantNameView.getText().toString();
		
		registrantUserNameView.setError(null);
		registrantUserName=registrantUserNameView.getText().toString();
		
		registrantPasswordView.setError(null);
		registrantPassword=registrantPasswordView.getText().toString();
		
		registrantConfirmPasswordView.setError(null);
		registrantConfirmPassword=registrantConfirmPasswordView.getText().toString();
		
		registrantEmailView.setError(null);
		registrantEmail=registrantEmailView.getText().toString();
		
		month=dp.getMonth();
		day=dp.getDayOfMonth();
		year=dp.getYear();
		String date=month+"-"+day+"-"+year;
		
		
		boolean cancel = false;
		View focusView = null;

		// Check for a valid email address.
		if(!android.util.Patterns.EMAIL_ADDRESS.matcher(registrantEmail).matches()){
			registrantEmailView.setError(getString(R.string.error_email_form));
			focusView = registrantEmailView;
			cancel = true;
		}
		
		// Check for a valid password.
		if(!registrantPassword.equals(registrantConfirmPassword)){
			registrantConfirmPasswordView.setError(getString(R.string.error_password_mismatch));
			registrantPasswordView.setError(getString(R.string.error_password_mismatch));
			focusView = registrantPasswordView;
			cancel = true;
		} if (TextUtils.isEmpty(registrantPassword)) {
			registrantPasswordView.setError(getString(R.string.error_field_required));
			focusView = registrantPasswordView;
			cancel = true;
		} else if (registrantPassword.length() < 4) {
			registrantPasswordView.setError(getString(R.string.error_invalid_password));
			focusView = registrantPasswordView;
			cancel = true;
		}

		
		// Check for a valid user name.
		if (TextUtils.isEmpty(registrantUserName)) {
			registrantUserNameView.setError(getString(R.string.error_field_required));
			focusView = registrantUserNameView;
			cancel = true;
		}
		else if (logins.contains(registrantUserName)) {
				// username already exists
				registrantUserNameView.setError(getString(R.string.error_username_exists));
				focusView = registrantUserNameView;
				cancel = true;
			}
		
		// Check for a valid registrant name.
		if (TextUtils.isEmpty(registrantName)) {
			registrantNameView.setError(getString(R.string.error_field_required));
			focusView = registrantNameView;
			cancel = true;
		}
		

		if (cancel) {
			// There was an error; don't attempt login and focus the first form field with an error.
			focusView.requestFocus();
		} else {
			// go to next page
			Intent result = new Intent();
			
			result.putExtra("n", registrantName);
			result.putExtra("d", date);
		    result.putExtra("un", registrantUserName);
		    result.putExtra("pw", registrantPassword);
		    result.putExtra("e", registrantEmail);
		    
			setResult(RESULT_OK,result);
			finish();
		}
		
		
		
		
	}
	
	public void cancel() {
		setResult(RESULT_CANCELED);
		finish();
	}
	

}
